<?php

/**
 * user actions.
 *
 * @package    sfwixet
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class profileActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  	try{
     $profile = Doctrine::getTable("Profile")->find($request->getParameter("id",$this->getUser()->getID()));
     	$user = $profile->getUser();
     	$profileArr = $profile->toArray();
     	$profileArr['interests'] = $profile->getInterests()->toArray();
     	$profileArr['firstName'] = $user->getFirstName();
     	$profileArr['lastName'] = $user->getLastName();
     	$profileArr['photoId'] = $user->getMainPhotoId();
     	echo json_encode($profileArr);
     }
     catch(AclException $e){
     	echo json_encode(array("error"=>true,"message"=>"Permission denied"));
     }
     
     return sfView::NONE;
  }
  
  public function executeNewBlog(sfWebRequest $request)
  {
  	  if($request->getParameter("title") != null && $request->getParameter("content") != null)
  	  {
  		$blog = new Blog();
  		$blog->setProfileId($this->getUser()->getId());
  		$blog->setTitle($request->getParameter("title"));
  		$blog->setContent($request->getParameter("content"));
  		$blog->save();
  		$this->getUser()->getGuardUser()->touch();
  		echo json_encode(array("error"=>false,"id"=>$blog->getBlogId()));
  	  }else echo json_encode(array("error"=>true,"message"=>_("Please provide a title and content")));
  	  
  	  return sfView::NONE;
  }
  
  public function executeGetBlog(sfWebRequest $request)
  {
  	if(is_numeric($request->getParameter("id")))
  		$profile = Doctrine::getTable("Profile")->find($request->getParameter("id",$this->getUser()->getId()));
  	else
  		$profile = $this->getUser()->getGuardUser()->getProfile();
  		
  		if($profile instanceof Profile){
  	  		$pager = $profile->getBlogs($request->getParameter("page",1),1);
	  		$results = $pager->getResults();
	  		if($results->count()>0)						
	  			echo json_encode(array("blog"=>$pager->getResults()->getFirst()->toArray(),"pagecount"=>$pager->getLastPage()));
	  		else
	  			echo json_encode(array("blog"=>array(),"pagecount"=>0));
  		}else echo json_encode(array("error"=>true,"message"=>_("You are not allowed to do this action")));
  	  
  	  return sfView::NONE;
  }
  
  public function executeRemoveBlog(sfWebRequest $request){
  	if(is_numeric($request->getParameter("id"))){
		$blog = Doctrine::getTable("Blog")->find($request->getParameter("id"));
		if($blog instanceof Blog && $blog->getProfileId() == $this->getUser()->getId()){
				$blog->delete();
				echo json_encode(array("error"=>false));
		}else echo json_encode(array("error"=>true,"message"=>_("You are not allowed to do this action")));
  	}else echo json_encode(array("error"=>true,"message"=>_("Please provide a blog id")));
  	return sfView::NONE;
  }
  
  
  public function executeGetComments(sfWebRequest $request)
  {
  	if(is_numeric($request->getParameter("id")))
  		$profile = Doctrine::getTable("Profile")->find($request->getParameter("id",$this->getUser()->getId()));
  	else
  		$profile = $this->getUser()->getGuardUser()->getProfile();
  		
  		if($profile instanceof Profile){
  	  		$pager = $profile->getComments($request->getParameter("page",1),20);
	  		  		$results = $pager->getResults();

  			$textComments = array();
			foreach ($pager->getResults() as $comment)
			{
				$textComment = array();
				$author = $comment->getAuthor();
				$authorData = array();
				
				$authorData['name']=$author->getFirstname()." ".$author->getLastname();
				$authorData['id']=$author->getId();
				$authorData['face']=$author->getFace()->getPhotoId();
				
				$textComment['comment'] = $comment->getComment();
				$textComment['author'] = $authorData;
				$textComment['date'] = $comment->getCreatedAt();
				
				$textComment['sound'] = $comment->getSound()->toArray();
				unset($textComment['sound']['uploader_id']);
				
				$textComments[] = $textComment;
			}
			
			//Put here to avoid notice the own comments
			$commentsNotifications = Doctrine::getTable("ProfileCommentNotification")->findByUserId($this->getUser()->getId());
			if($commentsNotifications)
				$commentsNotifications->delete();
			
  		echo json_encode(array("comments"=>$textComments,"pagecount"=>$pager->getLastPage()));
  		}else echo json_encode(array("error"=>true,"message"=>_("You are not allowed to do this action")));
  	  
  	  return sfView::NONE;
  }
  
  
  public function executeNewComment(sfWebRequest $request)
  {

  	if(is_numeric($request->getParameter("id")))
  		$profile = Doctrine::getTable("Profile")->find($request->getParameter("id",$this->getUser()->getId()));
  	else
  		$profile = $this->getUser()->getGuardUser()->getProfile();
  		
  		if($profile instanceof Profile){
  	  		$comment = new ProfileComment();
  	  		$comment->setOwnerId($profile->getUserId());
  	  		$comment->setAuthorId($this->getUser()->getId());
  	  		$content = trim($request->getParameter("content"));
  	  		
  	  		if(strlen($content)>0)
  	  		{
  	  			foreach($this->getUser()->getGuardUser()->getEmoticons() as $emoticon){
			  		$content = str_ireplace($emoticon->getShortKey(), "<img load='false' src='images/emoticons/".$emoticon->getId().".png' border='0'>", $content);
			  			
  				}
  				echo $content;
  				$comment->setComment($content);
  	  			$soundId = $request->getParameter("sound");
  	  			if($soundId){
  	  				$comment->setSoundId($soundId);
  	  			$soundFavouriteCounter = Doctrine::getTable("soundUser")->find(array($soundId,$this->getUser()->getId()));
  	  			if($soundFavouriteCounter instanceof soundUser)
  	  				$soundFavouriteCounter->setTimesUsed($soundFavouriteCounter->getTimesUsed()+1);
				else{
					$soundFavouriteCounter = new SoundUser();
					$soundFavouriteCounter->setSoundId($soundId);
					$soundFavouriteCounter->setUserId($this->getUser()->getId());
					$soundFavouriteCounter->setTimesUsed(1);
				}
  	  			$soundFavouriteCounter->save();
  	  			}
  	  			$comment->save();
  	  			$notification = new ProfileCommentNotification();
				$notification->setUserId($profile->getUserId());
				$notification->save();
				$profile->getUser()->touch();
  	  		}
  		}else echo json_encode(array("error"=>true,"message"=>_("You are not allowed to do this action")));
  	  
  	  return sfView::NONE;
  }
  
  public function executeUpdateInterest(sfWebRequest $request){
  	try{
	  	if($request->getParameter("id"))
	  	{
	  		$interest = Doctrine::getTable("Interest")->find($request->getParameter("id"));
	  		if($interest->getProfileId() == $this->getUser()->getId())
	  		{
	  			$interest->setContent($request->getParameter("content"));
	  			$interest->save();
	  			echo json_encode(array("error"=>false));	
	  		}else throw new Exception(_("You are not allowed to do this action"));
	  		 
	  	}
	  	else throw new Exception(_("Please specify an interest id"));
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	} 
  	
  	return sfView::NONE;
  }
  
  public function executeNewInterest(sfWebRequest $request){
  	try{
	  	if($request->getParameter("title"))
	  	{
	  		$interest = new Interest();
	  		$interest->setName($request->getParameter("title"));
	  		$interest->setContent($request->getParameter("content"));
	  		$interest->setProfileId($this->getUser()->getId());
	  		$interest->save();
	  		echo json_encode(array("error"=>false,"id"=>$interest->getInterestId()));	 
	  	}
	  	else throw new Exception(_("Please specify an interest title"));
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	} 
  	
  	return sfView::NONE;
  }
  
   public function executeDeleteInterest(sfWebRequest $request){
  	try{
	  	if($request->getParameter("id"))
	  	{
	  		$interest = Doctrine::getTable("Interest")->find($request->getParameter("id"));
	  		if($interest->getProfileId() == $this->getUser()->getId())
	  		{
	  			$interest->delete();
	  			echo json_encode(array("error"=>false));	
	  		}else throw new Exception(_("You are not allowed to do this action"));
	  		 
	  	}
	  	else throw new Exception(_("Please specify an interest id"));
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	} 
  	
  	return sfView::NONE;
  }
}
